package com.daybook.controller;

import com.daybook.pojo.*;
import com.daybook.service.AdminService;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping(value = "/adminMsg",method = RequestMethod.POST)
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping(value = "/Login",method = RequestMethod.POST)
    public String adminLogin(Admin admin, HttpSession session){
        System.out.print("sss");
    
            Admin admin1 = null;
            admin1 = adminService.selectByNameAndPwd(admin);
            if (admin1 != null){
                session.setAttribute("admin",admin1);
                return "redirect:/admin/index.jsp";
            }else {
                session.setAttribute("error","账号或者密码错误！");
                return "redirect:/admin/login.jsp";
            }
        
    	}
  //管理员注销
    @RequestMapping(value = "/Logout",method = RequestMethod.POST)
  		private void logout(HttpServletRequest request, HttpServletResponse response) throws  IOException, JSONException {
  			HttpSession session=request.getSession();
  			session.getAttribute("admin");
  			PrintWriter out = response.getWriter();
			JSONObject obj = new JSONObject();
			
  	        if(session!=null){ 
  	        	session.removeAttribute("admin");  
  	        	obj.put("res", 1);
  	        } else{
  	        	obj.put("res", -1);
  	        	
  	        }
  	      out.print(obj);
			out.close();	
  	       
  		}
    @RequestMapping(value = "/yzPwd",method = RequestMethod.POST)
	//管理员密码验证
		private void yzPwd(HttpServletRequest request, HttpServletResponse response) throws  IOException, JSONException {
			String pwd=request.getParameter("oldpwd");
			if(pwd.equals("")){
				pwd="1";
			}
    		HttpSession session=request.getSession();
			Admin admin=(Admin)session.getAttribute("admin");
			admin.setApwd(pwd);
			Admin oldadmin=adminService.selectByNameAndPwd(admin);
			PrintWriter out = response.getWriter();
			JSONObject obj = new JSONObject();
			if(oldadmin==null){
				obj.put("valid", false);
			}else{
				obj.put("valid", true);
			}
	        	out.print(obj);
				out.close();
	       
		}
    @RequestMapping(value = "/upAdminPwd",method = RequestMethod.POST)
   	//管理员密码验证
   		private void upAdminPwd(HttpServletRequest request, HttpServletResponse response) throws  IOException, JSONException {
   			String newpwd=request.getParameter("newpwd");
       		HttpSession session=request.getSession();
   			Admin admin=(Admin)session.getAttribute("admin");
   			int result=-1;
   			result=adminService.updateAdminByPwd(newpwd, admin.getAname());
   			if(result>0){
   				session.removeAttribute("admin");
   			}
   			PrintWriter out = response.getWriter();
   			JSONObject obj = new JSONObject();
   			obj.put("res", result);
   	        out.print(obj);
   			out.close();
   	       
   		}
  
}
